package hrm.action.insa.educationaction;

import hrm.action.Action;
import hrm.action.ActionForward;
import hrm.dto.insa.EducationDTO;
import hrm.dto.insa.EmployeeDTO;
import hrm.dto.login.LoginDTO;
import hrm.serviceimple.EducationServiceImple;
import hrm.serviceimple.EmployeeServiceImple;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InsaUpdateEducationAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward = new ActionForward();
		EducationServiceImple educationService = EducationServiceImple.getInstance();
		EmployeeServiceImple employeeService = EmployeeServiceImple.getInstance();
		EmployeeDTO dto = new EmployeeDTO();
		dto.setId(getFindId(request));
		dto = employeeService.getEmployee(dto);		
		
		EducationDTO edto = new EducationDTO();
		String educode = request.getParameter("educode");
		String eduschoolcode = request.getParameter("eduschoolcode");
		String edustartdate = request.getParameter("edustartdate");
		String edumajorcode = request.getParameter("edumajorcode");
		String graduationdate = request.getParameter("graduationdate");
		String isbranchschool = request.getParameter("isbranchschool");
		String daynight = request.getParameter("daynight");
		String edugrade = request.getParameter("edugrade");
		String totalgrade = request.getParameter("totalgrade");
		String joinbeforeafter = request.getParameter("joinbeforeafter");
		edto.setEducode(educode);
		edto.setEduschoolcode(eduschoolcode);
		edto.setEdustartdate(edustartdate);
		edto.setEdumajorcode(edumajorcode);
		edto.setGraduationdate(graduationdate);
		edto.setIsbranchschool(isbranchschool);
		edto.setDaynight(daynight);
		edto.setEdugrade(edugrade);
		edto.setTotalgrade(totalgrade);
		edto.setJoinbeforeafter(joinbeforeafter);
		edto.setId(getFindId(request));
		
		educationService.updateEducation(edto);
		List<EducationDTO> dtolist = educationService.getEducation(edto);		
				
		request.setAttribute("employeeDTO", dto);
		request.setAttribute("educationlist", dtolist);
		actfoward.setUrl("getEducation");
		return actfoward;
	}

	private String getFindId(HttpServletRequest request){
		
		String findid = (String) request.getSession().getAttribute("findid");
		if(findid == null){
			findid = ((LoginDTO)request.getSession().getAttribute("login")).getId();
			request.setAttribute("findid", findid);
		}
		return findid; 
	}
}
